package com.charm.businesshub.ui.activity

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.businesshub.adapter.NewsListAdapter
import com.charm.businesshub.anko.NewsListUI
import com.charm.businesshub.contract.NewsListView
import com.charm.businesshub.contract.presenter.NewsListPresenter
import com.charm.businesshub.entity.NewsListItemEntity
import com.charm.codes.mvp.factory.CreatePresenter
import com.charm.codes.mvp.view.AbstractMvpActivitiy
import org.jetbrains.anko.setContentView

/**
 * Created by tools on 2018/1/5.
 */
@Route(path = "/businesshub/newslist")
@CreatePresenter(NewsListPresenter::class)
class NewsListActivity : AbstractMvpActivitiy<NewsListView, NewsListPresenter>(), NewsListView {
    private lateinit var adapter: NewsListAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        adapter = NewsListAdapter(null)
        var newsListUI = NewsListUI(adapter)
        newsListUI.setContentView(this)
        newsListUI.setData("dida")
        initView()
        initData()
    }

    private fun initView() {
        adapter.setOnItemClickListener { adapter, _, position ->
            var item = adapter!!.data[position] as NewsListItemEntity
            ARouter.getInstance().build("/businesshub/detail")
                    .withString("content", item.content)
                    .navigation()
        }

    }

    private fun initData() {
        mvpPresenter.getNewsList()
    }

    override fun newsList(list: List<NewsListItemEntity>) {
        adapter.addData(list)
    }
}